#include "stdafx.h"
#include "VeidoGuide.h"
//#include "../../Resdef.h"
#include "resource.h"
#include "UIFactory.h"
#include "DrawBitmap.h"
#include "DrawButton.h"
#include "DrawLabel.h"
#include "ResourceHelper.h"
#include <shellapi.h>
namespace inception
{
    CVedioGuide::CVedioGuide() : pDialog_(NULL)
        , oldWndPrpc_(NULL)
    {
    }

    CVedioGuide::~CVedioGuide()
    {

    }

    void CVedioGuide::ShowPage( PageDataBase* pPageData )
    {
        if ( pPageData != NULL )
        {
            guideData_ = *(dynamic_cast<PageDataGuide*>(pPageData));
        }

        if ( pDialog_ != NULL )
        {
            if ( ::IsWindow(pDialog_->Get_HWnd()) )
            {
                HWND hWnd = pDialog_->Get_HWnd();
                if ( ::IsIconic(hWnd) )
                {
                    pDialog_->Show(SW_SHOWNORMAL);
                }
                else
                {
                    ::SetForegroundWindow( hWnd );
                }
            }
            else
            {
                delete pDialog_;
                CreateWnd();
            }
        }
        else 
        {
            CreateWnd();
        }
    }

    void CVedioGuide::CreateWnd()
    {
        RECT rcParent = {0};
        ::GetWindowRect(guideData_.hWnd, &rcParent);
        UIRect rcUIWnd( rcParent.left + guideData_.pt.x, rcParent.top + guideData_.pt.y, 420, 315 );

        RECT rcWnd = {0 };
        rcUIWnd.GetRECT(rcWnd);

        SIZE szWnd = { rcWnd.right - rcWnd.left, rcWnd.bottom - rcWnd.top };
        RECT rcCaption = {0, 0, 0 , 0 };
        pDialog_ = CUIFactory::Instance()->Get_Dialog();
        CustomObserver* pDlgObserver = new CustomObserver;
        UILib::PDialogParam pParam = new UILib::DialogParam;
        pParam->_hOwner = guideData_.hWnd;
        memcpy(&pParam->_rcWnd, &rcWnd, sizeof(RECT));
        memcpy(&pParam->_rcCaption, &rcCaption, sizeof(RECT));
        pDialog_->Set_CustomObserver( pDlgObserver );
        pDialog_->Set_BackgroundColor(0);
        pDialog_->Set_LayoutAttr(true);
        pDialog_->InitParam(pParam);

        CDrawUITree* pTree = new CDrawUITree( pDialog_->Get_HWnd() );

        pDialog_->BindUITree(pTree);
        pDialog_->Show( SW_SHOWNORMAL );

        Bitmap* pbmpBkg = CResourceHelper::GetInstance()->GetBitmapFromID(IDP_LIVE_MASK);
        UIRect rcBkg( 0,0, szWnd.cx, szWnd.cy );
        CDrawBitmap* pBkg = new CDrawBitmap(pbmpBkg, &rcBkg, NULL, Draw_FillRect);
        pTree->AddChildren(pBkg);

        if ( oldWndPrpc_ == NULL )
        {
            ::SetProp( guideData_.hWnd, L"TrayWnd", (HANDLE)this );
            oldWndPrpc_ = ( WNDPROC )::SetWindowLongPtr( guideData_.hWnd, GWL_WNDPROC, ( LONG_PTR )WindowProc );
        }
    }

    void CVedioGuide::OnClick( const UINT uID )
    {
        switch ( uID )
        {
        default:
            break;
        }
    }

    void CVedioGuide::OnClose()
    {
        if ( pDialog_ )
        {
            ::SetProp(guideData_.hWnd, L"TrayWnd", NULL );
            ::SetWindowLongPtr( guideData_.hWnd, GWL_WNDPROC, ( LONG_PTR )oldWndPrpc_ );
            oldWndPrpc_ = NULL;
            ::SendMessage(pDialog_->Get_HWnd(), WM_CLOSE, 0, 0);
        }
    }

    LRESULT CALLBACK CVedioGuide::WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
    {
        CVedioGuide* pWnd = (CVedioGuide*)(::GetProp(hwnd, L"TrayWnd" ));

        if ( pWnd == NULL )
            return S_OK;
        //Message
        switch( uMsg )
        {
        case WM_SHOWWINDOW:
        case WM_MOVE:
        case WM_MOVING:
        case WM_SIZE:
        case WM_WINDOWPOSCHANGED:
            {
                pWnd->OnTraceMove(uMsg, wParam, lParam);
            }
            break;
        }
        if ( pWnd->oldWndPrpc_ != NULL )
        {
            return CallWindowProc( pWnd->oldWndPrpc_, hwnd, uMsg, wParam, lParam );
        }
        else
        {
            return DefWindowProc(hwnd, uMsg, wParam, lParam );
        }
    }

    void CVedioGuide::OnTraceMove( UINT uMsg, WPARAM wParam, LPARAM lParam )
    {
        HWND hWnd = pDialog_->Get_HWnd();

        RECT rcParent = {0};
        ::GetWindowRect(guideData_.hWnd, &rcParent);
        
        POINT pt = { rcParent.left + guideData_.pt.x, rcParent.top + guideData_.pt.y };
        SetWindowPos( hWnd, NULL, pt.x, pt.y, 0, 0, SWP_NOSIZE);
        if ( ::IsWindowVisible(hWnd) !=  ::IsWindowVisible(guideData_.hWnd) )
        {
            BOOL bShow = ::IsWindowVisible(guideData_.hWnd);
            DWORD dwSHow = bShow ? SW_SHOWNORMAL : SW_HIDE;
            ::ShowWindow(hWnd, dwSHow);
        }
    }
}